library(MASS)
library(tidyverse)
library(stargazer)
library(sandwich)


setwd("C:/Users/lucas/OneDrive/Desktop/Dino Project/MA Data")

# Load primary analysis data
IndustryType<-read_csv("Data/Industry.csv")
NetworkData<-read_csv("Data/NetworkData.csv")
MergerAcquisitionData<-read_csv("Data/MergerAcquisitionData.csv")


# Join Data Sets
CompleteDataSet<-left_join(MergerAcquisitionData,NetworkData, by=c('orgId','YearBins'))
CompleteDataSet<-left_join(CompleteDataSet,IndustryType,by='orgId')

# Create Industry Variables
CompleteDataSet$IndustryID<-as.factor(CompleteDataSet$IndustryID)
CompleteDataSet$Finance<-ifelse(CompleteDataSet$IndustryID=="Finance",1,0)
CompleteDataSet$Manufacturing<-ifelse(CompleteDataSet$IndustryID=="Manufacturing",1,0)
CompleteDataSet$Retail<-ifelse(CompleteDataSet$IndustryID=="Retail",1,0)

# Convert Strings to factors
CompleteDataSet$YearBins<-as.factor(CompleteDataSet$YearBins)
CompleteDataSet$orgId<-as.factor(CompleteDataSet$orgId)

# Create Lags of Mergers & Acquistions
CompleteDataSet<-CompleteDataSet%>%
  group_by(orgId)%>%
  mutate(lagMA2=lag(lagMA1))%>%
  mutate(lagMA3=lag(lagMA2))

# Create lags of total mergers, acquisitions and partial acquisitions
CompleteDataSet<-CompleteDataSet%>%
  group_by(orgId)%>%
  mutate(lagM=lag(TotalMergerBin),lagA=lag(TotalAcqBin),lagPA=lag(TotalPartAcqBin))

# Normalize Betweenness Score
CompleteDataSet$BTN <- (CompleteDataSet$BT - mean(CompleteDataSet$BT, na.rm=T)) / sd(CompleteDataSet$BT, na.rm=T)

##########################################
# Judicial Data Sample
##########################################
Signers_By_Year<-read_csv("Data/Signers_By_Year.csv")


# Generate the network plots for Figure B1 in Appendix
GenerateNetwrokPlot<-function(Start,End,binSize,data,binNumber, NodeColor,PlotTitle,NodeSize=4){
  YearCuts<-seq(End,Start, by=-binSize)
  YearLabels<-seq(1,length(YearCuts)-1)
  YearBins<-cut(data$Year, breaks = YearCuts, labels = YearLabels)
  data$YearBins<-as.integer(YearBins)
  
  data<-data%>%filter(YearBins<=binNumber)
  
  colors<-c("#51A3A3","#CB904D")
  AdjMatrix<-crossprod(table(data[2:3]))
  diag(AdjMatrix)<-0
  AdjMatrix<-as.matrix(AdjMatrix)
  Graph<-graph_from_adjacency_matrix(AdjMatrix, mode=c("undirected"))
  NC<-ifelse(V(Graph)$name%in%NodeColor,1,0)
  V(Graph)$NodeColor=NC
  Plot<-plot(Graph, vertex.size=NodeSize,
             vertex.label=NA, 
             layout=layout_with_kk(Graph)*0.25,
             vertex.color=c( "#F39C6B","#878E88")[1+(V(Graph)$NodeColor==0)] ,
             edge.curved=.05,
             main=PlotTitle, frame=T)
  return(Plot)
}


par(mfrow=c(2,2))
GenerateNetwrokPlot(Start = 1940, End = 2012, binSize = 5, data=Signers_By_Year, binNumber = 3, NodeColor=unique(final_results$orgId), PlotTitle = "1950s", NodeSize=6)
GenerateNetwrokPlot(Start = 1940, End = 2012, binSize = 5, data=Signers_By_Year, binNumber = 7, NodeColor=unique(final_results$orgId), PlotTitle = "1970s", NodeSize=6)
GenerateNetwrokPlot(Start = 1940, End = 2012, binSize = 5, data=Signers_By_Year, binNumber = 10, NodeColor=unique(final_results$orgId), PlotTitle = "1990s")
GenerateNetwrokPlot(Start = 1940, End = 2012, binSize = 5, data=Signers_By_Year, binNumber = 13, NodeColor=unique(final_results$orgId), PlotTitle = "2010s")



##########################################
# Alternative Lagged Specification
##########################################

# Table D1 in Appendix

M3<-glm.nb(issueNumber~TotalMABin+lagMA1+YearBins+orgId,data=CompleteDataSet,control = glm.control(maxit = 100000))
M2<-lm(winRate~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalMABin+lagMA1+YearBins+orgId,data=CompleteDataSet)
M4<-lm(EC~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)
M5<-lm(BTN~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)
M6<-glm.nb(DG~TotalMABin+lagMA1+YearBins+orgId,data=CompleteDataSet,control = glm.control(maxit = 100000))
M7<-lm(HC~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)

stargazer::stargazer(M1,M2,M3,M4,M5,M6,M7, 
                     star.cutoffs = c(0.05), 
                     model.names = FALSE,
                     single.row = F,
                     omit = c('Year','orgId'),
                     keep.stat = c('n','aic','adj.rsq'),
                     notes="$^{*}$p$<$0.05",
                     notes.append = F,
                     style = 'ajps',
                     covariate.labels = c('Num. M&As','Lag M&As','Constant'),
                     dep.var.labels=c( "Num. Briefs", "Win Rate", "Num. Issues", "Eigenvector Cent.","Betweenness","Degree","Harmonic Centr."),
                     add.lines = list(c('Year FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Company FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Court FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Model Type','NB','OLS','NB','OLS','OLS','NB','OLS')),
                     type='text')




# Table D2 in Appendix

M3<-glm.nb(issueNumber~TotalMABin+lagMA1+lagMA2+lagMA3+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M2<-lm(winRate~TotalMABin+lagMA1+lagMA2+lagMA3+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalMABin+lagMA1+lagMA2+lagMA3+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M4<-lm(EC~TotalMABin+lagMA1+lagMA2+lagMA3+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M5<-lm(BTN~TotalMABin+lagMA1+lagMA2+lagMA3+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M6<-glm.nb(DG~TotalMABin+lagMA1+lagMA2+lagMA3+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M7<-lm(HC~TotalMABin+lagMA1+lagMA2+lagMA3+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)


stargazer::stargazer(M1,M2,M3,M4,M5,M6,M7, 
                     star.cutoffs = c(0.05), 
                     model.names = FALSE,
                     single.row = F,
                     omit = c('as\\.','orgId', 'Year'),
                     keep.stat = c('n','aic','adj.rsq'),
                     notes="$^{*}$p$<$0.05",
                     notes.append = F,
                     style = 'ajps',
                     covariate.labels = c('Num. M\\&As',' 1$-$Lag M\\&As',' 2$-$Lag M\\&As',' 3$-$Lag M\\&As'),
                     add.lines = list(c('Year FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Company FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Model Type','NB','OLS','NB','OLS','OLS','NB','OLS')),
                     dep.var.labels=c( "Num. Briefs", "Win Rate", "Num. Issues", "Eigenvector Cent.","Betweenness","Degree","Harmonic Cent."),
                     type='text')


######################################
# Alternative Model Controls
######################################



# Table D3 in Appendix

M3<-glm.nb(issueNumber~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(CourtLabel),data=CompleteDataSet)
M2<-lm(winRate~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(CourtLabel),  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(CourtLabel),data=CompleteDataSet)
M4<-lm(EC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(CourtLabel),  data=CompleteDataSet)
M5<-lm(BTN~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(CourtLabel),  data=CompleteDataSet)
M6<-glm.nb(DG~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(CourtLabel),data=CompleteDataSet)
M7<-lm(HC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(CourtLabel),  data=CompleteDataSet)

stargazer::stargazer(M1,M2,M3,M4,M5,M6,M7, 
                     star.cutoffs = c(0.05), 
                     model.names = FALSE,
                     single.row = F,
                     omit = "as\\.",
                     keep.stat = c('n','aic','adj.rsq'),
                     notes="$^{*}$p$<$0.05",
                     notes.append = F,
                     style = 'ajps',
                     covariate.labels = c('Num. M\\&As',' Lag M\\&As'),
                     add.lines = list(c('Year FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Company FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Court FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Model Type','NB','OLS','NB','OLS','OLS','NB','OLS')),
                     dep.var.labels=c( "Num. Briefs", "Win Rate", "Num. Issues", "Eigenvector Cent.","Betweenness","Degree","Harmonic Cent."),
                     type='text')


# Table D4 in Appendix

M3<-glm.nb(issueNumber~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(IndustryID),data=CompleteDataSet)
M2<-lm(winRate~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(IndustryID),  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(IndustryID),data=CompleteDataSet)
M4<-lm(EC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(IndustryID),  data=CompleteDataSet)
M5<-lm(BTN~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(IndustryID),  data=CompleteDataSet)
M6<-glm.nb(DG~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(IndustryID),data=CompleteDataSet)
M7<-lm(HC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId)+as.factor(IndustryID),  data=CompleteDataSet)

stargazer::stargazer(M1,M2,M3,M4,M5,M6,M7, 
                     star.cutoffs = c(0.05), 
                     model.names = FALSE,
                     single.row = F,
                     omit = "as\\.",
                     keep.stat = c('n','aic','adj.rsq'),
                     notes="$^{*}$p$<$0.05",
                     notes.append = F,
                     style = 'ajps',
                     covariate.labels = c('Num. M\\&As',' Lag M\\&As'),
                     add.lines = list(c('Year FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Company FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Industry FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Model Type','NB','OLS','NB','OLS','OLS','NB','OLS')),
                     dep.var.labels=c( "Num. Briefs", "Win Rate", "Num. Issues", "Eigenvector Cent.","Betweenness","Degree","Harmonic Cent."),
                     type='text')


############################################
# Alternative M&A Codings
############################################

# Table D5 in Appendix

M3<-glm.nb(issueNumber~TotalAcqBin+TotalMergerBin+TotalPartAcqBin+lagA+lagM+lagPA+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M2<-lm(winRate~TotalAcqBin+TotalMergerBin+TotalPartAcqBin+lagA+lagM+lagPA+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalAcqBin+TotalMergerBin+TotalPartAcqBin+lagA+lagM+lagPA+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M4<-lm(EC~TotalAcqBin+TotalMergerBin+TotalPartAcqBin+lagA+lagM+lagPA+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M5<-lm(BTN~TotalAcqBin+TotalMergerBin+TotalPartAcqBin+lagA+lagM+lagPA+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M6<-glm.nb(DG~TotalAcqBin+TotalMergerBin+TotalPartAcqBin+lagA+lagM+lagPA+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M7<-lm(HC~TotalAcqBin+TotalMergerBin+TotalPartAcqBin+lagA+lagM+lagPA+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)

stargazer::stargazer(M1,M2,M3,M4,M5,M6,M7, 
                     star.cutoffs = c(0.05), 
                     model.names = FALSE,
                     single.row = F,
                     omit = "as\\.",
                     keep.stat = c('n','aic','adj.rsq'),
                     notes="$^{*}$p$<$0.05",
                     notes.append = F,
                     style = 'ajps',
                     covariate.labels = c('Num. Acquisitions','Num. Mergers','Num. Partial Acq.','Lagged Acquistions','Lagged Mergers','Lagged Part. Acquistions'),
                     add.lines = list(c('Year FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Company FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                                      c('Model Type','NB','OLS','NB','OLS','OLS','NB','OLS')),
                     dep.var.labels=c( "Num. Briefs", "Win Rate", "Num. Issues", "Eigenvector Cent.","Betweenness","Degree","Harmonic Cent."),
                     type='text')




########################################
# The Net Effect of M&As 
########################################

# Figure D1 in Appendix
Plottting_Data <-  read_csv("Data/Company_MA_Brief_Total.csv")


Issue_Areas<-ggplot(Plottting_Data) +
  geom_point(aes(x = Total_MA, y = Issue_num), position='jitter') +
  theme_bw() +
  xlab("Number of M&As") +
  ylab("")+
  ggtitle("Issue Areas")+
  geom_smooth(aes(x = Total_MA, y = Issue_num), method = 'loess', formula = y ~ x, linetype='dashed', color='#716B5B', alpha=0.25)+
  theme(plot.title = element_text(hjust = 0.5, face="bold"))

Total_Briefs<-ggplot(Plottting_Data) +
  geom_point(aes(x = Total_MA, y = Briefs), position='jitter') +
  theme_bw() +
  xlab("Number of M&As") +
  ylab("")+
  ggtitle("Briefs")+
  geom_smooth(aes(x = Total_MA, y = Briefs), method = 'loess', formula = y ~ x, linetype='dashed', color='#716B5B', alpha=0.25)+
  theme(plot.title = element_text(hjust = 0.5, face="bold"))

Cosigners<-ggplot(Plottting_Data) +
  geom_point(aes(x = Total_MA, y = Cosigners)) +
  theme_bw() +
  xlab("Number of M&As") +
  ylab("")+
  ggtitle("Cosigners")+
  geom_smooth(aes(x = Total_MA, y = Cosigners),formula = y ~ x,method = 'loess', linetype='dashed', color='#716B5B', alpha=0.25)+
  theme(plot.title = element_text(hjust = 0.5, face="bold"))

Win_Rate<-ggplot(Plottting_Data) +
  geom_point(aes(x = Total_MA, y = Win_Rate)) +
  theme_bw() +
  xlab("Number of M&As") +
  ylab("")+
  geom_hline(yintercept = 0.50, linetype='dashed', alpha=0.50)+
  ggtitle("Case Win Rate")+
  geom_smooth(aes(x = Total_MA, y = Win_Rate), method = 'loess', formula = y ~ x, linetype='dashed', color='#716B5B', alpha=0.25)+
  theme(plot.title = element_text(hjust = 0.5, face="bold"))

########################################
# Industry Specific Effects
########################################

# Figure D2 in Appendix

ggplot(CompleteDataSet%>%group_by(IndustryID)%>%summarise(n=mean(winRate,na.rm=T))%>%filter(!is.na(IndustryID)))+
  geom_col(aes(x=n,y=IndustryID), alpha=0.75, color='black')+
  xlab("\nAverage Win-Rate From 1950-Present")+
  ylab('Industry Type\n')+
  theme_bw()+
  theme(axis.text = element_text(face = 'bold',size=10),
        axis.title = element_text(face='bold',size=12),
        axis.text.x = element_text(size = 9))

# Figure D3 in Appendix

ggplot(CompleteDataSet%>%group_by(IndustryID)%>%filter(MAIndicator>0)%>%tally()%>%filter(!is.na(IndustryID)))+
  geom_col(aes(x=n,y=IndustryID), alpha=0.75, color='black')+
  xlab("\nTotal Number of Briefs Signed")+
  ylab('Industry Type\n')+
  theme_bw()+
  theme(axis.text = element_text(face = 'bold',size=10),
        axis.title = element_text(face='bold',size=12),
        axis.text.x = element_text(size = 9))


# Figure D4 in Appendix

# Run models from primary analysis
M3<-glm.nb(issueNumber~TotalMABin+lagMA1+YearBins+orgId,data=CompleteDataSet,control = glm.control(maxit = 100000))
M2<-lm(winRate~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalMABin+lagMA1+YearBins+orgId,data=CompleteDataSet)
M4<-lm(EC~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)
M5<-lm(BTN~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)
M6<-glm.nb(DG~TotalMABin+lagMA1+YearBins+orgId,data=CompleteDataSet,control = glm.control(maxit = 100000))
M7<-lm(HC~TotalMABin+lagMA1+YearBins+orgId,  data=CompleteDataSet)


# Extracts FE from OLS models
ExploreFE_LM<-function(M){
  modelSummary<-summary(M)
  modelSummary<-data.frame(modelSummary$coefficients)
  modelSummary$Name<-row.names(modelSummary)
  modelSummary<-modelSummary%>%
    filter(!grepl("Year",Name))%>%
    mutate(Name=gsub('orgId',"",Name))%>%
    filter(Pr...t..<=0.05)%>%
    rename(Pr=Pr...t..,
           value=t.value)
  return(modelSummary)
}

# Extracts FE from NB models
ExploreFE_NB<-function(M){
  modelSummary<-summary(M)
  modelSummary<-data.frame(modelSummary$coefficients)
  modelSummary$Name<-row.names(modelSummary)
  modelSummary<-modelSummary%>%
    filter(!grepl("Year|Intercept",Name))%>%
    mutate(Name=gsub('orgId',"",Name))%>%
    filter(Pr...z..<=0.05)%>%
    rename(Pr=Pr...z..,
           value=z.value)
  return(modelSummary)
}

# Extract FE from models
M1_FE<-ExploreFE_NB(M1)%>%mutate(Type="Num. Briefs")
M2_FE<-ExploreFE_LM(M2)%>%filter(Estimate>0.65)%>%mutate(Type="Win Rate")
M3_FE<-ExploreFE_NB(M3)%>%mutate(Type="Num. Issues")
M4_FE<-ExploreFE_LM(M4)%>%mutate(Type="Eigenvector Cent.")
M5_FE<-ExploreFE_LM(M5)%>%mutate(Type="Betweenness")
M6_FE<-ExploreFE_NB(M6)%>%filter(Estimate>0)%>%mutate(Type="Degree")
M7_FE<-ExploreFE_LM(M7)%>%filter(Estimate>mean(Estimate))%>%mutate(Type="Harmonic Cent.")

# Join FE Datasets
M_FE<-rbind(M1_FE,M2_FE,M3_FE,M4_FE,M5_FE,M6_FE,M7_FE)
M_FE$Name<-as.numeric(M_FE$Name)
M_FE<-left_join(M_FE, IndustryType, by=c('Name' ='orgId'))

# Add outcome details
M_FE$Type<-as.factor(M_FE$Type)
M_FE$Type<-fct_relevel(M_FE$Type, c("Num. Briefs","Num. Issues","Win Rate","Eigenvector Cent.","Betweenness","Degree","Harmonic Cent."))

# Plot Figure D4
iTypes<-M_FE%>%
  filter(!is.na(IndustryID))%>%
  group_by(IndustryID, Type)%>%
  tally()
ggplot()+
  geom_bar(data = iTypes, aes(y=n,x=IndustryID, fill=Type), stat = 'identity',alpha=0.5, position = position_dodge(), color='black')+
  labs(y='Number of Companies\n',x="\nCompany Industry Area", fill='Dep. Variable')+
  scale_fill_manual(values=c("#F7C59F","#7FB685","#DE3C4B","#2A324B","#767B91","#C7CCDB","#E1E5EE" ))+
  theme_bw()+
  theme(axis.text = element_text(face = 'bold',size=10),
        axis.title = element_text(face='bold',size=12),
        axis.text.x = element_text(size = 9),
        legend.position = "bottom")




#######################################
# Alternative Year Bins
#######################################

# 10 Year Bins
# Load alternative year binned data 
MergerAcquisitionData<-read_csv("Data/MergerAcquisitionData_10_Year.csv")
NetworkData<-read_csv("Data/NetworkData_10_Year.csv")



# Join Data Sets
CompleteDataSet<-left_join(MergerAcquisitionData,NetworkData, by=c('orgId','YearBins'))
CompleteDataSet<-left_join(CompleteDataSet,IndustryType,by='orgId')

# Create Industry Variables
CompleteDataSet$IndustryID<-as.factor(CompleteDataSet$IndustryID)
CompleteDataSet$Finance<-ifelse(CompleteDataSet$IndustryID=="Finance",1,0)
CompleteDataSet$Manufacturing<-ifelse(CompleteDataSet$IndustryID=="Manufacturing",1,0)
CompleteDataSet$Retail<-ifelse(CompleteDataSet$IndustryID=="Retail",1,0)

# Convert Strings to factors
CompleteDataSet$YearBins<-as.factor(CompleteDataSet$YearBins)
CompleteDataSet$orgId<-as.factor(CompleteDataSet$orgId)

# Create Lags of Mergers & Acquistions
CompleteDataSet<-CompleteDataSet%>%
  group_by(orgId)%>%
  mutate(lagMA2=lag(lagMA1))%>%
  mutate(lagMA3=lag(lagMA2))

# Create lags of total mergers, acquisitions and partial acquisitions
CompleteDataSet<-CompleteDataSet%>%
  group_by(orgId)%>%
  mutate(lagM=lag(TotalMergerBin),lagA=lag(TotalAcqBin),lagPA=lag(TotalPartAcqBin))

# Normalize Betweenness Score
CompleteDataSet$BTN <- (CompleteDataSet$BT - mean(CompleteDataSet$BT, na.rm=T)) / sd(CompleteDataSet$BT, na.rm=T)

M3<-glm.nb(issueNumber~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M2<-lm(winRate~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M4<-lm(EC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M5<-lm(BTN~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M6<-glm.nb(DG~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M7<-lm(HC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)

# Table C3 in Appendix
stargazer(M1,M2,M3,M4,M5,M6,M7,
          star.cutoffs = c(0.05), 
          model.names = FALSE,
          single.row = F,
          omit = "as\\.",
          keep.stat = c('n','aic','adj.rsq'),
          notes="$^{*}$p$<$0.05",
          notes.append = F,
          style = 'ajps',
          
          add.lines = list(c('Year FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                           c('Company FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                           c('Model Type','NB','OLS','NB','OLS','OLS','NB','OLS')),
          dep.var.labels=c( "Num. Briefs", "Win Rate", "Num. Issues", "Eigenvector Cent.","Betweenness","Degree","Harmonic Cent."),
          type='text')


# 15 Year Bins
# Load alternative year binned data 
MergerAcquisitionData<-read_csv("Data/MergerAcquisitionData_15_Year.csv")
NetworkData<-read_csv("Data/NetworkData_15_Year.csv")



# Join Data Sets
CompleteDataSet<-left_join(MergerAcquisitionData,NetworkData, by=c('orgId','YearBins'))
CompleteDataSet<-left_join(CompleteDataSet,IndustryType,by='orgId')

# Create Industry Variables
CompleteDataSet$IndustryID<-as.factor(CompleteDataSet$IndustryID)
CompleteDataSet$Finance<-ifelse(CompleteDataSet$IndustryID=="Finance",1,0)
CompleteDataSet$Manufacturing<-ifelse(CompleteDataSet$IndustryID=="Manufacturing",1,0)
CompleteDataSet$Retail<-ifelse(CompleteDataSet$IndustryID=="Retail",1,0)

# Convert Strings to factors
CompleteDataSet$YearBins<-as.factor(CompleteDataSet$YearBins)
CompleteDataSet$orgId<-as.factor(CompleteDataSet$orgId)

# Create Lags of Mergers & Acquistions
CompleteDataSet<-CompleteDataSet%>%
  group_by(orgId)%>%
  mutate(lagMA2=lag(lagMA1))%>%
  mutate(lagMA3=lag(lagMA2))

# Create lags of total mergers, acquisitions and partial acquisitions
CompleteDataSet<-CompleteDataSet%>%
  group_by(orgId)%>%
  mutate(lagM=lag(TotalMergerBin),lagA=lag(TotalAcqBin),lagPA=lag(TotalPartAcqBin))

# Normalize Betweenness Score
CompleteDataSet$BTN <- (CompleteDataSet$BT - mean(CompleteDataSet$BT, na.rm=T)) / sd(CompleteDataSet$BT, na.rm=T)

M3<-glm.nb(issueNumber~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M2<-lm(winRate~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M1<-glm.nb(TotalBriefBin~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M4<-lm(EC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M5<-lm(BTN~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)
M6<-glm.nb(DG~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),data=CompleteDataSet)
M7<-lm(HC~TotalMABin+lagMA1+as.factor(YearBins)+as.factor(orgId),  data=CompleteDataSet)

# Table C4 in Appendix
stargazer(M1,M2,M3,M4,M5,M6,M7,
          star.cutoffs = c(0.05), 
          model.names = FALSE,
          single.row = F,
          omit = "as\\.",
          keep.stat = c('n','aic','adj.rsq'),
          notes="$^{*}$p$<$0.05",
          notes.append = F,
          style = 'ajps',
          
          add.lines = list(c('Year FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                           c('Company FE','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark','\\checkmark'),
                           c('Model Type','NB','OLS','NB','OLS','OLS','NB','OLS')),
          dep.var.labels=c( "Num. Briefs", "Win Rate", "Num. Issues", "Eigenvector Cent.","Betweenness","Degree","Harmonic Cent."),
          type='text')